import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ActivatedRoute, Params } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import {
    IEntityDtoOfGuid,
    PagedCategoryDto,
    UpdateCategoryInput, WorkFlowDesignServiceProxy
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/api';
import { Paginator } from 'primeng/paginator';
import { Table } from 'primeng/table';
import { finalize } from 'rxjs/operators';
import { CreateOrEditFlowCategoryComponent } from './create-or-edit-workflow-category.component';

@Component({
    templateUrl: './work-flow-category.component.html',
    animations: [appModuleAnimation()]
})
export class WorkFlowCategoryComponent extends AppComponentBase {

    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;
    @ViewChild('createOrEditModal',{static: true}) createOrEditModal:CreateOrEditFlowCategoryComponent;


    advancedFiltersAreShown = false;
    filterText = '';
    kind: number;
    selectedDatas: IEntityDtoOfGuid[] = [];
    constructor(
        injector: Injector,
        private _activatedRoute: ActivatedRoute,
        private _workFlowDesignServiceProxy:WorkFlowDesignServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        this._activatedRoute.params.subscribe((params: Params) => {
            this.kind = Number(params['kind']);
        });
    }


    getListData(event?: LazyLoadEvent) {
        this.selectedDatas = [];
        if (this.primengTableHelper.shouldResetPaging(event)) {
            if (this.primengTableHelper.TryChangePage(this.paginator, 0)) {
                return;
            }
        }

        this.primengTableHelper.showLoadingIndicator();
        this._workFlowDesignServiceProxy.getPagedCategory(this.filterText,this.primengTableHelper.getSorting(this.dataTable),
        this.primengTableHelper.getMaxResultCount(this.paginator, event),
        this.primengTableHelper.getSkipCount(this.paginator, event)).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }

    delete(){
        if (!this.selectedDatas) return;
        let ids = this.selectedDatas.map(item=>item.id);
        if (ids.length == 0) return;
        this.message.confirm(this.l('DeleteWarningMessage'), this.l('AreYouSure'),
        (isConfirmed) => {
            if (!isConfirmed) return;
            this._workFlowDesignServiceProxy.deleteCategory(ids)
                .subscribe(() => {
                    this.reloadPage();
                    this.notify.success(this.l('SuccessfullyDeleted'));
                });
        })
    }



    reloadPage() {
        if (this.primengTableHelper.TryChangePage(this.paginator, this.paginator.getPage())) {
            return;
        }

        this.getListData(null);
    }

    createWorkFlowCategory(){
        this.createOrEditModal.show(null);
    }

    editWorkFlowCategroy(record:PagedCategoryDto){
        this.createOrEditModal.show(record);
    }

}
